Running Javascript Outside the Browser
We’re gonna interact with Node using the command line. And for that, I’m gonna use the built-in terminal that VS Code has, hitting control back take for that. So this command that you see here, actually. Or you can go to View and Terminal.
Now if you prefer to use another application for the terminal, that’s fine as well but throughout this lecture, I’m always going to use the terminal that is built in right into VS Code so that we don’t have to jump around between different windows so much.
Now, to start writing some Node code here in the console, all we have to do is to write node, given that you have Node.js installed on your computer and then, just hit enter.
And this will then open up the Node REPL, which stands for read-eval-print loop. So basically here, we can write JavaScript code just like in a normal terminal. For example, we can define variables. And so, here it is. So, we just defined the name variable. And in fact, we can write any JavaScript code that we like in here, okay? So, that’s because at the end of the day, Node.js is really just a JavaScript run time, as we just saw before.
So what else can we do?
Well, any type of expression really, will work. Let’s just do some math here, for example. Okay so any JavaScript will work and you see that we actually defined this variable here using const so that is ES6 and that is absolutely no problem in Node.js. So it supports ES6 in all the newer versions out of the box without any problem. That’s because we’re not running this JavaScript in any browser, but it will always run on the server, okay? So, in fact, we just took JavaScript out of the browser and we’re running it inside of this Node application.
Now if we wanted to exit JS REPL, so again, this read-eval-print loop, that Node gives us, there are different ways of doing that. The first one is to write exit or actually .exit and so this exited this Node process, this REPL and so that started again so that I can show you some more stuff. So again, just type Node, hit enter, and that’s it.
Oh, and, by the way, if you want to clear your terminal like I just did, all you have to do is to hit command K and that will then clear the command line and probably on Windows, that is control K, okay? Anyway, we use the .exit to exit the REPL but we can also hit control D, and that’s not command, it is really control, so control D will do the same. But, let’s quickly enter it again ’cause there’s some more stuff that I want to show you.
So if you hit tab right now, maybe you have to tap it twice, sometimes that happens for some reason, but anyway, by hitting tab, you can see all the global variables that are available in Node. So you have all the kind of stuff that we’re already used to like the Array constructor or the String constructor or Math or Number over here, but then there’s also all kinds of stuff that belongs to Node. For example, your https and fs or crypto and these are Node modules that we’re gonna talk more about a bit later in the section. But for now, you see that we have all kinds of global variables that we can access whenever we want in Node.js, okay?
Also, another nice trick is the underscore variable. So let me first show you something. So another calculation for example. Three times eight gives 24 and now was can use underscore plus six and so this will give us 30 and so that means that underscore is basically your previous result. So we had 24, and so underscore here is 24 now and 24 plus six makes 30. So if we now did underscore minus 30, that will give us zero, right? And yeah, it did.
Okay, and finally, the tab that you just pressed before, you can also press that, for example, on one of these constructor that we already know like string. So String. and now by adding tab, you can see all the methods or properties that are available to us. Again, sometimes you have to hit it twice but then here we are. So we have, for example, length or hasOwnProperty, and all these kinds of methods here that we know already, right?